package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;

import com.clqteam.model.Employee;
import com.clqteam.service.EmployeeLoginService;
import com.clqteam.service.ShowrNameService;
import com.clqteam.service.UpdateEmployeeService;
import com.clqteam.serviceimpl.EmployeeLoginServiceImpl;
import com.clqteam.serviceimpl.ShowrNameServiceImpl;
import com.clqteam.serviceimpl.UpdateEmployeeServiceImpl;
/**
 * 
 * @author CLQ
 *
 */
public class UpdateEmployeeController extends HttpServlet{
	 UpdateEmployeeService upService=new UpdateEmployeeServiceImpl();
	 ShowrNameService rname=new ShowrNameServiceImpl();
	 EmployeeLoginService emService=new EmployeeLoginServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	doPost(req, resp);
}
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	//设置字符集
	   req.setCharacterEncoding("utf8");
       resp.setCharacterEncoding("utf8");
       String account=req.getParameter("eAccount1");
       String psw=req.getParameter("ePsw1");
       String name=req.getParameter("eName1");
       String n=req.getParameter("rname1");
       //将字符串类型数据转换为基本数据类型
          int na;
          try {
			   na=Integer.parseInt(n);
		} catch (NumberFormatException e) {
			    na=rname.swichrIdByrName(n);
		}
       int eid=Integer.parseInt(req.getParameter("eId1"));
        boolean b=upService.updateEmployeeInfo(account, psw, name, eid, na);
        PrintWriter p=resp.getWriter();
       if(b){
    	   p.write("1");
    	   HttpSession session=req.getSession();
    	   Employee emp=(Employee)session.getAttribute("employee");
    	   SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	   Date date=new Date();
    	   String time=sd.format(date);  
 	    	String drInfo="成功修改员工："+name+"的基本信息";
 	    	emService.insertLoginTime(time, drInfo, emp.geteId(),emp.geteName());
       }else{
    	   p.write("0");
       }
       p.flush();
       p.close();
	}
	
}
